<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Layout (View Helper)
 *
 * @package    helper
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_View_Helper_Layout extends EGP_View_Helper_Abstract implements IteratorAggregate
{

    /**
     * layout 数据存储
     *
     * @var EGP_ArrayObject
     */
    public static $layout = null;

    /**
     * layout 名称
     *
     * @var string
     */
    public $name = null;

    /**
     * 初始化方法
     *
     * @param  string  $name
     * @return EGP_View_Helper_Layout
     */
    public function init($name)
    {
        $this->name = (string) $name;
        null == self::$layout && self::$layout = new EGP_ArrayObject();
        if (!self::$layout->offsetExists($this->name))
            self::$layout->offsetSet($this->name, new EGP_ArrayObject());
        $this->view->layout = self::$layout;
        return $this;
    }

    /**
     * 设置 layout 属性
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return EGP_View_Helper_Layout
     */
    public function set($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $v)
                $this->set($k, $v);
        } else {
            self::$layout->offsetGet($this->name)->offsetSet($key, $value);
            $this->view->layout = self::$layout;
        }
        return $this;
    }

    /**
     * 获取 IteratorAggregate 接口
     *
     * @return EGP_ArrayObject
     */
    public function getIterator()
    {
        return self::$layout[$this->name];
    }

    /**
     * 获取 layout 属性
     *
     * @param  string  $key
     * @return mixed
     */
    public function get($key)
    {
        return $this->has($key) ? self::$layout->offsetGet($this->name)->offsetGet($key) : null;
    }

    /**
     * 是否存在指定属性
     *
     * @param  string  $key
     * @return boolean
     */
    public function has($key)
    {
        return self::$layout->offsetGet($this->name)->offsetExists($key);
    }

    /**
     * 删除指定属性
     *
     * @param  string  $key
     * @return EGP_View_Helper_Layout
     */
    public function del($key)
    {
        self::$layout->offsetGet($this->name)->offsetUnset($key);
        $this->view->layout = self::$layout;
        return $this;
    }

    /**
     * 魔术方法 - 设置 layout 属性
     *
     * @param  string  $key
     * @param  mixed   $value
     */
    public function __set($key, $value)
    {
        $this->set($key, $value);
    }

    /**
     * 魔术方法 - 获取 layout 属性
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->get($key);
    }

    /**
     * 魔术方法 - 判断是否存在指定属性
     *
     * @param  string  $key
     * @return boolean
     */
    public function __isset($key)
    {
        return $this->has($key);
    }

    /**
     * 魔术方法 - 删除指定属性
     *
     * @param  string  $key
     */
    public function __unset($key)
    {
        $this->del($key);
    }

}